package cjq.demo.thread.ch4_11;

import java.util.concurrent.*;

/**
 * Created by Administrator on 2016/5/1.
 */
public class Main {

    public static void main(String[] args) {
        ExecutorService executor = (ExecutorService) Executors.newCachedThreadPool();

        CompletionService<String> service = new ExecutorCompletionService<>(executor);

        ReportRequest faceRequest = new ReportRequest("Face",service);

        ReportRequest onlineRequest = new ReportRequest("Online",service);
        Thread faceThread = new Thread(faceRequest);
        Thread onlineThread = new Thread(onlineRequest);

        ReportProcessor processor = new ReportProcessor(service);
        Thread senderThread = new Thread(processor);

        System.out.println("Main: starting the Threads");
        faceThread.start();
        onlineThread.start();
        senderThread.start();

        try{
            System.out.println("Main: Waiting for the report generators");
            faceThread.join();
            onlineThread.join();
        }catch(InterruptedException e){
            e.printStackTrace();
        }

        System.out.println("Main: Shutting down the executor ");
        executor.shutdown();
        try{
            executor.awaitTermination(1, TimeUnit.SECONDS.DAYS);

        }catch(InterruptedException e){
            e.printStackTrace();
        }

        processor.setEnd(true);

        System.out.println("Main: ends");
    }
}
